package com.swjsj.hsk;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 发送普通文本格式email
 * 名称：	SendSimpleEmail
 * 描述：
 * 创建人：	吉仕军
 * 创建时间：	2011-6-27 上午10:22:35
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @version 1.0
 */
@Component
public class SendSimpleEmail {

    public final static Logger log = LoggerFactory.getLogger(SendSimpleEmail.class);

    //邮件编码
    @Value("${emailCode}")
    private String emailCode;
    //邮件服务器
    @Value("${mailHost}")
    private String mailHost;
    @Value("${user}")
    private String user;
    @Value("${password}")
    private String password;
    //收件人的列表key：mail value:用户名 不填为空
    private Map<String, String> to = new HashMap<String, String>();
    //发件人邮箱 用户名
    private Map<String, String> from = new HashMap<String, String>();
    //主题
    private String subject;
    //文本内容
    private String msg;

    @Value("${port}")
    private int port;

    @Value("${toStr}")
    private String toStr;

    @Value("${fromStr}")
    private String fromStr;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }


    //private SimpleEmail email = new SimpleEmail();

    //public SendSimpleEmail(String subject, String msg) {
    //    this.subject = subject;
    //    this.msg = msg;
    //}

    public SimpleEmail getSimpleEmail() {
        return new SimpleEmail();
    }

    /**
     * 通过配置文件初始化发送邮件
     * 构造方法 SendSimpleEmail.
     *
     * @param
     */
    public void getInitDataByConfig() {
        String[] ts = toStr.split(";");
        for (int i = 0; i < ts.length; i++) {
            String[] toInfo = ts[i].split("\\|");
            if (toInfo.length == 2) {
                to.put(toInfo[0], toInfo[1]);
            } else if (toInfo.length == 1) {
                to.put(toInfo[0], toInfo[0]);
            }
        }
        String[] froms = fromStr.split("\\|");
        if (froms.length == 2) {
            from.put(froms[0], froms[1]);
        } else if (froms.length == 1) {
            from.put(froms[0], froms[0]);
        }
    }


    /**
     * 初始化邮件发送信息
     *
     * @throws EmailException
     */
    public void initSend(SimpleEmail email) throws EmailException {

        if (StringUtils.isNotNull(email.getHostName())) {
            return;
        }
        // 设置邮件编码
        email.setCharset(emailCode);
        // 设置邮件服务器
        email.setHostName(mailHost);

        if (port == 25 || port == 0) {
            email.setSmtpPort(25);
        } else {
            email.setSSLOnConnect(Boolean.TRUE);
            email.setSslSmtpPort(port + "");
        }

        // 设置登录邮件服务器用户名和密码
        email.setAuthentication(user, password);


        // 添加收件人
        Iterator<String> it = to.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            String value = to.get(key);
            email.addTo(key, value);
        }
        // 设置发件人
        Iterator<String> it1 = from.keySet().iterator();
        while (it1.hasNext()) {
            String key = it1.next();
            String value = from.get(key);
            email.setFrom(key, value);
        }
    }


    public void send() throws EmailException {

        SimpleEmail email = getSimpleEmail();

        initSend(email);

        // 设置邮件标题
        email.setSubject(subject);
        // 设置邮件正文内容
        email.setMsg(msg);
        // 发送邮件
        email.send();

    }


    public String getEmailCode() {
        return emailCode;
    }


    public void setEmailCode(String emailCode) {
        this.emailCode = emailCode;
    }


    public String getMailHost() {
        return mailHost;
    }


    public void setMailHost(String mailHost) {
        this.mailHost = mailHost;
    }


    public String getUser() {
        return user;
    }


    public void setUser(String user) {
        this.user = user;
    }


    public String getPassword() {
        return password;
    }


    public void setPassword(String password) {
        this.password = password;
    }


    public Map<String, String> getTo() {
        return to;
    }


    public void setTo(Map<String, String> to) {
        this.to = to;
    }


    public Map<String, String> getFrom() {
        return from;
    }


    public void setFrom(Map<String, String> from) {
        this.from = from;
    }


    public String getSubject() {
        return subject;
    }


    public void setSubject(String subject) {
        this.subject = subject;
    }


    public String getMsg() {
        return msg;
    }


    public void setMsg(String msg) {
        this.msg = msg;
    }



}
